package com.cc.service.generate;


import cn.hutool.core.collection.CollectionUtil;
import com.cc.build.BuilderInter;
import com.cc.config.PackagePathConfiguration;
import com.cc.config.TemplateConfiguration;
import com.cc.pojo.file.BuiderAbstract;
import com.cc.pojo.file.FilePathAndClassName;
import com.cc.pojo.tablestruct.LibaryProperties;
import com.cc.service.core.GenerateAbstract;
import com.cc.utils.beanUtil.BeanUtils;
import com.cc.utils.ConvertHumpUnderlinesToEachOtherUtil;
import com.cc.utils.CoreUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author c.c
 * @Description 核心生成类
 * @since 2023/3/30
 */

public class GenerateInter extends GenerateAbstract {
  private PackagePathConfiguration packagePathConfiguration = BeanUtils.getBean(PackagePathConfiguration.class);
  private TemplateConfiguration templateConfiguration = BeanUtils.getBean(TemplateConfiguration.class);

  public GenerateInter() {
    this.getTargetPackageAddress();
  }

  @Override
  public List<FilePathAndClassName<BuiderAbstract>> getTargetPackageAddress(Object... youParameter) {
    List<FilePathAndClassName<BuiderAbstract>> packageAddress = new ArrayList<>();
    List<LibaryProperties> libaryProperties = super.getLibaryProperties();
    libaryProperties.forEach(libaryProperty -> {
      if (CollectionUtil.isNotEmpty(libaryProperty.getTableStructs())) {
        StringBuilder packgePath = new StringBuilder();
        String path = CoreUtil.getPath(libaryProperty, packgePath, packagePathConfiguration.isInterisLastLayer(), packagePathConfiguration.getInterPath());
        //获取db名
        String classnameForDb = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true) + "Db";
        //获取dao名
        String classnameForDao = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true) + "Dao";
        //获取inter名
        String classnameForInter = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true) + "Inter";

        List<String> paths = Arrays.asList(super.getPathAddress(classnameForDao), super.getPathAddress(classnameForDb));
        BuilderInter builderInter = new BuilderInter(packgePath.toString(), paths, classnameForDao, classnameForInter, classnameForDb);
        super.putPathAddress(classnameForInter, packgePath);
        FilePathAndClassName<BuiderAbstract> filePathAndClassName = new FilePathAndClassName<>(path, classnameForInter, builderInter);
        packageAddress.add(filePathAndClassName);
      }
    });
    super.build(packageAddress, templateConfiguration.getInterPath());
    return packageAddress;
  }
}
